/**
 * 
 */
package com.feonix.common.util;

import org.apache.commons.lang.ArrayUtils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @CopyRight (c)1993-2017
 * @Project m-complat-common
 * @Comments 汉语拼音工具类
 * @NameSpace com.feonix.common.util
 * @Author Feonixzw
 * @CreateDate 2017年12月7日
 */
public class PinyinUtil {

	/**
	 * 将字符串转换成拼音数组
	 * 
	 * @param src
	 * @return
	 */
	public static String[] stringToPinyin(String src) {
		return stringToPinyin(src, false, null);
	}

	/**
	 * 将字符串转换成拼音数组
	 * 
	 * @param src
	 * @param separator
	 * @return
	 */
	public static String[] stringToPinyin(String src, String separator) {
		return stringToPinyin(src, true, separator);
	}

	/**
	 * 将字符串转换成拼音数组
	 * 
	 * @param src
	 * @param isPolyphone
	 *            是否查出多音字的所有拼音
	 * @param separator
	 *            多音字拼音之间的分隔符
	 * @return
	 */
	public static String[] stringToPinyin(String src, boolean isPolyphone, String separator) {
		if (("".equals(src)) || (src == null)) {
			return null;
		}
		char[] srcArray = src.toCharArray();
		int i = srcArray.length;
		String[] pinyinResult = new String[i];

		for (int j = 0; j < i; ++j) {
			pinyinResult[j] = charToPinyin(srcArray[j], isPolyphone, separator);
		}
		return pinyinResult;
	}

	/**
	 * 将单个字符转换成拼音
	 * 
	 * @param src
	 * @param isPolyphone
	 * @param separator
	 * @return
	 */
	public static String charToPinyin(char src, boolean isPolyphone, String separator) {
		HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();

		hanyuPinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

		StringBuffer sb = new StringBuffer();

		if (src > 128) {
			try {
				String[] strArray = PinyinHelper.toHanyuPinyinStringArray(src, hanyuPinyinOutputFormat);
				if ((isPolyphone) && (separator != null)) {
					for (int i = 0; i < strArray.length; i++) {
						sb.append(strArray[i]);
						if (strArray.length != i + 1) {
							sb.append(separator);
						}
					}
				} else {
					sb.append(strArray[0]);
				}
			} catch (BadHanyuPinyinOutputFormatCombination localBadHanyuPinyinOutputFormatCombination) {
				localBadHanyuPinyinOutputFormatCombination.printStackTrace();
			}
		} else {
			sb.append(src);
		}
		return sb.toString();
	}

	/**
	 * 将汉字转换成拼音 无分隔符
	 * 
	 * @param hanzi
	 * @return
	 */
	public static String hanziToPinyin(String hanzi) {
		return hanziToPinyin(hanzi, "");
	}

	/**
	 * 将汉字转换成拼音
	 * 
	 * @param hanzi
	 * @param separator
	 * @return
	 */
	public static String hanziToPinyin(String hanzi, String separator) {
		HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
		hanyuPinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		String str = "";
		try {
			str = PinyinHelper.toHanYuPinyinString(hanzi, hanyuPinyinOutputFormat, separator, false);
		} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
			badHanyuPinyinOutputFormatCombination.printStackTrace();
		}
		return str;
	}

	/**
	 * 将字符串数组转换成字符串
	 * 
	 * @param str
	 * @param separator
	 *            各个字符串之间的分隔符
	 * @return
	 */
	public static String stringArrayToString(String[] str, String separator) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < str.length; ++i) {
			sb.append(str[i]);
			if (str.length != i + 1) {
				sb.append(separator);
			}
		}
		return sb.toString();
	}

	/**
	 * 简单的将各个字符数组之间连接起来
	 * 
	 * @param str
	 * @return
	 */
	public static String stringArrayToString(String[] str) {
		return stringArrayToString(str, "");
	}

	/**
	 * 将字符数组转换成字符串
	 * 
	 * @param ch
	 * @param separator
	 * @return
	 */
	public static String charArrayToString(char[] ch, String separator) {
		StringBuffer localStringBuffer = new StringBuffer();
		for (int i = 0; i < ch.length; ++i) {
			localStringBuffer.append(ch[i]);
			if (ch.length != i + 1) {
				localStringBuffer.append(separator);
			}
		}
		return localStringBuffer.toString();
	}

	/**
	 * 将字符数组转换成字符串
	 * 
	 * @param ch
	 * @return
	 */
	public static String charArrayToString(char[] ch) {
		return charArrayToString(ch, " ");
	}

	/**
	 * 取汉字的首字母
	 * 
	 * @param src
	 * @param isCapital
	 *            是否大写
	 * @return
	 */
	public static char[] getHeadByChar(char src, boolean isCapital) {
		if (src <= 128) {
			return new char[] { src };
		}
		String[] arrayOfString1 = PinyinHelper.toHanyuPinyinStringArray(src);
		int i = arrayOfString1.length;
		char[] arrayOfChar = new char[i];
		int j = 0;
		for (String str : arrayOfString1) {
			char c = str.charAt(0);
			if (isCapital)
				arrayOfChar[j] = Character.toUpperCase(c);
			else {
				arrayOfChar[j] = c;
			}
			++j;
		}

		return arrayOfChar;
	}

	/**
	 * 取汉字的首字母(默认是大写)
	 * 
	 * @param src
	 * @return
	 */
	public static char[] getHeadByChar(char src) {
		return getHeadByChar(src, true);
	}

	/**
	 * 查找字符串首字母
	 * 
	 * @param src
	 * @return
	 */
	public static String getHeadByString(String src) {
		return getHeadByString(src, true);
	}

	/**
	 * 查找字符串首字母
	 * 
	 * @param src
	 * @param isCapital
	 *            是否大写
	 * 
	 * @return
	 */
	public static String getHeadByString(String src, boolean isCapital) {
		return getHeadByString(src, isCapital, null);
	}

	/**
	 * 
	 * @param src
	 * @param isCapital
	 *            是否大写
	 * @param separator
	 *            分隔符
	 * @return
	 */
	public static String getHeadByString(String src, boolean isCapital, String separator) {
		char[] arrayOfChar1 = src.toCharArray();
		String[] arrayOfString = new String[arrayOfChar1.length];
		int i = 0;
		for (char c1 : arrayOfChar1) {
			try {
				char[] arrayOfChar3 = getHeadByChar(c1, isCapital);
				StringBuffer localStringBuffer = new StringBuffer();
				if (separator != null) {
					int l = 1;

					for (char c2 : arrayOfChar3) {
						localStringBuffer.append(c2);
						if (l != arrayOfChar3.length) {
							localStringBuffer.append(separator);
						}
						++l;
					}
				} else {
					localStringBuffer.append(arrayOfChar3[0]);
				}
				arrayOfString[i] = localStringBuffer.toString();
			} catch (Exception localException) {
				arrayOfString[i] = "";
			}
			++i;
		}
		String str = "";
		if (ArrayUtils.isNotEmpty(arrayOfString)) {
			str = StringUtil.join(arrayOfString, separator);
		}
		return str;
	}

}
